﻿using SIIE_ERP.BaseDatos;
using SIIE_ERP.Codigo;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Elysium.Demo.Registro
{
    /// <summary>
    /// Lógica de interacción para Window1.xaml
    /// </summary>
    public partial class WindowRegistroRutaBD : Elysium.Controls.Window
    {
        public WindowRegistroRutaBD()
        {
            InitializeComponent();           
        }

        private void Button_Seleccionar_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            fbd.Description = "Elija la carpeta donde desea guardar su información";
            System.Windows.Forms.DialogResult result = fbd.ShowDialog();

            string fName = "Informacion_SIEPyMEs.accdb";

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    File.Copy(System.IO.Path.Combine(Directory.GetCurrentDirectory() + "\\_ArchivosParaBin\\", fName), System.IO.Path.Combine(fbd.SelectedPath + "\\", fName), true);
                    SrvDB.SetDatabasePath(fbd.SelectedPath + "\\" + fName, "config.xml");
                    ScriptInicial.DoScriptInicial();
                    Textbox_Ruta.Text = fbd.SelectedPath;
                }
                catch (Exception)
                {
                    MessageBox.Show("La ruta elegida no está disponible.");
                    return;
                }                
            }
            else
            {
                MessageBox.Show("Se canceló la Selección.");                
            }
        }

        private void Button_Guardar_Copy_Click(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrEmpty(Textbox_Ruta.Text))
            {
                try
                {
                    OleDbConnection myConnection = new OleDbConnection(SrvDB.GetDefaultConnection());
                    myConnection.Open();

                    OleDbCommand command = new OleDbCommand("SELECT * FROM dbo_Cat_Parametros_Sistema", myConnection);
                    OleDbDataAdapter da = new OleDbDataAdapter(command);
                    DataSet dset = new DataSet();
                    da.Fill(dset);

                    //CloseAllWindows();
                    WindowRegistroUsuario InfoUser = new WindowRegistroUsuario();
                    InfoUser.Show();
                    this.Hide();
                }
                catch (Exception)
                {
                    MessageBox.Show("No se puede obtener permisos para acceder al archivo Informacion_SIEPyMEs.accdb, facor de seleccionar otra ruta.");
                    return;
                }
            }
            else
            {
                MessageBox.Show("Favor de seleccionar una carpeta.");
            }
        }

        private void CloseAllWindows()
        {
            for (int intCounter = App.Current.Windows.Count - 1; intCounter > 0; intCounter--)
                App.Current.Windows[intCounter].Close();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown(0); // Cerrar definitivamente la aplicacion
        }
    }
}
